<?php
    /**
     * Created by JetBrains PhpStorm.
     * User: porterzhang
     * Date: 13-3-5 下午4:44
     * To change this template use File | Settings | File Templates.
     */
    if (!defined('IN_MOBILE_API')) {
        exit('Access Denied');
    }
    include_once 'forum.php';
    class cloud_api {
        function common() { }

        function output() {
            $variable = array();
            //判断是否缺少参数
            if (empty($_GET['mobiles']) || empty($_GET['caid'])) {
                $variable = array(
                    'code' => 1001,
                    'messagestr' => lang('plugin/cloud', '1001'),
                );
                cloud_core::result(cloud_core::variable($variable));
            }
            //判断用户是否存在
            $data = C::t('#cloud#cloud_users')->fetch_first_by_mobile($_GET['mobiles'],$_GET['caid']);
            if(empty($data)){
                $variable = array(
                    'code' => 5000,
                    'messagestr' => lang('plugin/cloud', '5000'),
                );
                cloud_core::result(cloud_core::variable($variable));
            }else{
                $request = C::t('#cloud#cloud_message_status')->fetch_user_send_message_count($_GET['mobiles']);
                if($request['count'] >= 3){
                    $variable = array(
                        'code' => 100000,
                        'messagestr' => lang('plugin/cloud', '100000'),
                    );
                    cloud_core::result(cloud_core::variable($variable));
                }else{
                    $http = 'http://api.sms7.cn/mt/'; //短信接口
                    $uid = 'xayoudao'; //用户账号
                    $pwd = '1qazxsw2@yd'; //密码
                    $mobile = $_GET['mobiles']; //号码
                    $mobileids = $mobile.$_GET['caid']; //号码唯一编号
                    $code = self::random(6);
                    $content = '找回密码的验证码：'.$code; //内容
                    //即时发送
                    $res = self::sendSMS($http, $uid, $pwd, $mobile, $content, $mobileids);
                    if($res=='success'){
                        //正常返回
                        $variable = array(
                            'data' => array('codes'=>$code),
                        );
                        cloud_core::result(cloud_core::variable($variable));
                    }else{
                        $variable = array(
                            'code' => $res,
                        );
                        cloud_core::result(cloud_core::variable($variable));
                    }
                }
            }
        }
        function random($len) {
            $srcstr = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
            mt_srand();
            $sstrs = "";

            for ($i = 0; $i < $len; $i++) {
                $strs .= $srcstr[mt_rand(0, 35)];
            }

            return strtoupper($strs);
        }
        function sendSMS($http, $uid, $pwd, $mobile, $content, $mobileids, $time = '', $mid = '') {
            $data = array
            (
                'uid' => $uid, //用户账号
                'pwd' => md5($pwd . $uid), //MD5位32密码,密码和用户名拼接字符
                'mobile' => $mobile, //号码
                'content' => $content, //内容
                'mobileids' => $mobileids,
                'time' => $time, //定时发送
                'encode'=>'utf8',
            );
            $re = self::postSMS($http, $data); //POST方式提交
            parse_str($re);
            if ($stat == '100') {
                return "success";
            } else {
                return "error：" . $re;
            }
        }

        function postSMS($url, $data = '') {
            $row = parse_url($url);
            $host = $row['host'];
            $port = 80;
            $file = $row['path'];
            $post='';
            while (list($k, $v) = each($data)) {
                $post .= rawurlencode($k) . "=" . rawurlencode($v) . "&"; //转URL标准码
            }
            $post = substr($post, 0, -1);
            $len = strlen($post);
            $fp = @fsockopen($host, $port, $errno, $errstr, 10);
            if (!$fp) {
                return "$errstr ($errno)\n";
            } else {
                $receive = '';
                $out = "POST $file HTTP/1.1\r\n";
                $out .= "Host: $host\r\n";
                $out .= "Content-type: application/x-www-form-urlencoded\r\n";
                $out .= "Connection: Close\r\n";
                $out .= "Content-Length: $len\r\n\r\n";
                $out .= $post;
                fwrite($fp, $out);
                while (!feof($fp)) {
                    $receive .= fgets($fp, 128);
                }
                fclose($fp);
                $receive = explode("\r\n\r\n", $receive);
                unset($receive[0]);
                return implode("", $receive);
            }
        }
    }

?>

